package org.example.netDevlop;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author xuan
 * @create 2023/10/12
 */
public class API {
    public static void main(String[] args) throws UnknownHostException {
        //1.获取本机的InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);//SKUSER-ICQOUU76/10.40.4.215  (前面是主机名，后面是主机ip地址)

        //2. 根据指定主机名（主机名也可以理解成是域名） 获取 InetAddress对象
        InetAddress host1 = InetAddress.getByName("SKUSER-ICQOUU76");
        System.out.println(host1);//SKUSER-ICQOUU76/10.40.4.215  (前面是主机名或域名，后面是主机ip地址)

        //3. 根据域名返回 InetAddress对象, 比如 www.baidu.com 对应
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println(host2);//www.baidu.com/180.101.50.188

        //4. 通过 InetAddress 对象，获取对应的ip地址
        String hostAddress = host2.getHostAddress();//IP 180.101.50.188
        System.out.println("host2 对应的ip = " + hostAddress);//180.101.50.188

        //5. 通过 InetAddress 对象，获取对应的主机名/也可以说是域名
        String hostName = host2.getHostName();
        System.out.println("host2对应的主机名/也可以说是域名=" + hostName); // www.baidu.com
    }
}
